/*
 *此javaCode由YMS-JDBC替换工具生成，原mapper位置com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.mapper.CoderfidMapper
 */
package com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.mapper;

import com.yonyou.iuap.yms.api.IYmsJdbcApi;
import com.yonyou.iuap.yms.param.*;
import com.yonyou.iuap.yms.param.ResultSetProcessor;
import com.yonyou.iuap.yms.processor.*;
import com.yonyou.iuap.yms.replace.CollectionUtil;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.entity.coderfid;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.model.CoderfidViewBean;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class CoderfidMapperImpl {

    @Autowired
    private IYmsJdbcApi ymsJdbcApi;

    public int InsertCoderfid(String epc_tid, String rfid_code, String product_id, String product_name, String product_sku_id, String product_sku_name, String unitId, String unit_name, String stockUnitId, String stock_unit_name, Double f_stock_count, String warehouse_id, String warehouse_code, String tenant_id, String creator, String modifier, String id, String batchno, String productDate) throws Exception {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "INSERT INTO `dxq_coderfid`(`epc_tid`,`rfid_code`,`product_id`,product_name,`product_sku_id`,product_sku_name, unitId,unit_name,stockUnitId,stock_unit_name,`f_stock_count`,warehouse_id,warehouse_code,`rfid_status`,`tenant_id`,`ytenant_id`,`create_time`,`modify_time`,`creator`,`modifier`,id,batchno,productDate)\n" + 
			         "values(?,?,?,?,?,?,?,?,?,?,?,?,?, 0,?,?,now(),now(),?,?,?,?,?)                                                                                                                                                                                                                                                        \n";
        parameter.addParam(epc_tid);
		parameter.addParam(rfid_code);
		parameter.addParam(product_id);
		parameter.addParam(product_name);
		parameter.addParam(product_sku_id);
		parameter.addParam(product_sku_name);
		parameter.addParam(unitId);
		parameter.addParam(unit_name);
		parameter.addParam(stockUnitId);
		parameter.addParam(stock_unit_name);
		parameter.addParam(f_stock_count);
		parameter.addParam(warehouse_id);
		parameter.addParam(warehouse_code);
		parameter.addParam(tenant_id);
		parameter.addParam(tenant_id);
		parameter.addParam(creator);
		parameter.addParam(modifier);
		parameter.addParam(id);
		parameter.addParam(batchno);
		parameter.addParam(productDate);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int Updatechukupick(List<String> ids, String modifier, String pickuser, String rfid_status, String tenant_id) throws Exception {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" update dxq_coderfid set rfid_status=?,modify_time=now(),modifier=?,pickuser=? ");
		parameter.addParam(rfid_status);
		parameter.addParam(modifier);
		parameter.addParam(pickuser);
		sqlBuilder.append(" where ytenant_id=? and dr=0 ");
		parameter.addParam(tenant_id);
		if (CollectionUtil.isNotEmpty(ids)) {
			sqlBuilder.append(" and id in ");
			sqlBuilder.append(" ( ");
			String separator0 = " , ";
			for (int index = 0; index < ids.size(); index++) {
				String item = ids.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < ids.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}else{
			sqlBuilder.append(" and id in ('') ");
		}
        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int deleteCoderfid(String id, String modifier, String tenant_id) throws Exception {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "update dxq_coderfid set dr=1,modify_time=now(),modifier=? where id=? and ytenant_id=? and dr=0\n";
        parameter.addParam(modifier);
		parameter.addParam(id);
		parameter.addParam(tenant_id);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int deleteCoderfidByIds(List<String> ids, String tenant_id) throws Exception {
		if (ids == null || ids.size()==0) return 0;
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" delete from dxq_coderfid where ytenant_id=? ");
		parameter.addParam(tenant_id);
		if (CollectionUtil.isNotEmpty(ids)) {
			sqlBuilder.append(" and id in ");
			sqlBuilder.append(" ( ");
			String separator0 = " , ";
			for (int index = 0; index < ids.size(); index++) {
				String item = ids.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < ids.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}else{
			sqlBuilder.append(" and id in ('')");
		}

        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int CleanCoderfid(String id, String modifier, String tenant_id) throws Exception {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "update dxq_coderfid set shelf_id='',location_id='', modify_time=now(),modifier=?,rfid_status=0 where id=? and ytenant_id=? and dr=0\n";
        parameter.addParam(modifier);
		parameter.addParam(id);
		parameter.addParam(tenant_id);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public coderfid getFRIDCode(String epccode, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from dxq_coderfid where rfid_status=1 and rfid_code = ? and ytenant_id = ? limit 1\n";
        parameter.addParam(epccode);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanProcessor(new coderfid());
        return (coderfid) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public List<CoderfidViewBean> SelcoderfidListBylo(String product_id, String product_sku_id, String warehouse_id, String batchno, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		//rfid_status库存状态:0入库未上架-分配完标签后的状态 1在库正常 2停用 3标签损坏-1删除 4拣货 5配货 6发货
		//rfid_status：1,0 可以拣货的物料状态 提示拣货位置(提示货架信息)
		sqlBuilder.append(" select *,'' as locationName from dxq_coderfid where product_id=? and product_sku_id=? and warehouse_id=? and rfid_status in (1,0) and ytenant_id=? and (!ISNULL(location_id) and location_id!='') ");
		parameter.addParam(product_id);
		parameter.addParam(product_sku_id);
		parameter.addParam(warehouse_id);
		parameter.addParam(tenant_id);
		if (batchno != null && !"".equals(batchno)) {
			sqlBuilder.append(" and batchno=? ");
			parameter.addParam(batchno);
		}
		sqlBuilder.append(" and dr=0 order by invalidDate asc; ");

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new CoderfidViewBean());
        return (List<CoderfidViewBean>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }

    public int UpdatechukuDel(List<String> strList, String tenant_id) throws Exception {
		if (strList == null || strList.size()==0) return 0;
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" delete from dxq_coderfid where ytenant_id=? and dr=0 ");
		parameter.addParam(tenant_id);
		if (CollectionUtil.isNotEmpty(strList)) {
			sqlBuilder.append(" and id in ");
			sqlBuilder.append(" ( ");
			String separator0 = " , ";
			for (int index = 0; index < strList.size(); index++) {
				String item = strList.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < strList.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}else{
			sqlBuilder.append(" and id in ('')");
		}

        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int updateRFIDCode(String epccode, Double count, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "update dxq_coderfid set f_stock_count=?,is_split=1 where rfid_code=? and ytenant_id=?\n";
        parameter.addParam(count);
		parameter.addParam(epccode);
		parameter.addParam(tenant_id);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public List<coderfid> SelcoderfidList(String product_id, String product_sku_id, String warehouse_id, String tenant_id) {
        SQLParameter parameter = new SQLParameter(true);
		//库存状态:0入库未上架-分配完标签后的状态 1在库正常 2停用 3标签损坏-1删除 4拣货 5配货 6发货
        //rfid_status：1,3,0应发货物料的库存状态
        String sql = "select * from dxq_coderfid where product_id=? and product_sku_id=? and warehouse_id=? and dr=0 and rfid_status in (1,3,0) and ytenant_id=?\n";
        parameter.addParam(product_id);
		parameter.addParam(product_sku_id);
		parameter.addParam(warehouse_id);
		parameter.addParam(tenant_id);

        ResultSetProcessor processor = new AutoBeanListProcessor(new coderfid());
        return (List<coderfid>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public int Updatechuku(List<String> strList, String modifier, String rfid_status, String tenant_id) throws Exception {
		if (strList == null || strList.size()==0) return 0;
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" update dxq_coderfid set rfid_status=?,modify_time=now(),modifier=? ");
		parameter.addParam(rfid_status);
		parameter.addParam(modifier);
		sqlBuilder.append(" where ytenant_id=? and dr=0 ");
		parameter.addParam(tenant_id);
		if (CollectionUtil.isNotEmpty(strList)) {
			sqlBuilder.append(" and id in ");
			sqlBuilder.append(" ( ");
			String separator0 = " , ";
			for (int index = 0; index < strList.size(); index++) {
				String item = strList.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < strList.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}else{
			sqlBuilder.append(" and id in ('')");
		}

        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }

	/**
	 * 盘盈盘亏时对库存的处理
	 * @param ids
	 * @param modifier
	 * @param rfid_status  如果盘点的状态为亏，需要将数据库rfid_status修改为6
	 * @param tenant_id
	 * @return
	 * @throws Exception
	 */
	public int UpdateYingKui(List<String> ids, String modifier, String rfid_status, String tenant_id) throws Exception {
		if (ids == null || ids.size()==0) return 0;
		SQLParameter parameter = new SQLParameter(true);
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append("update dxq_coderfid set modify_time=now(), modifier=?");
		parameter.addParam(modifier);
		if(!rfid_status.equals("")){
			sqlBuilder.append(",rfid_status=? ");
			parameter.addParam(rfid_status);
		}
		sqlBuilder.append(" where ytenant_id=? and dr=0 ");
		parameter.addParam(tenant_id);
		if (CollectionUtil.isNotEmpty(ids)) {
			sqlBuilder.append(" and id in ");
			sqlBuilder.append(" ( ");
			String separator0 = " , ";
			for (int index = 0; index < ids.size(); index++) {
				String item = ids.get(index);
				sqlBuilder.append(" ? ");
				parameter.addParam(item);
				if (index < ids.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
			sqlBuilder.append(" ) ");
		}else{
			sqlBuilder.append(" and id in ('')");
		}

		String sql = sqlBuilder.toString();

		return ymsJdbcApi.update(sql, parameter);
	}
}
